home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Finder ProgressBar 1.1 / Finder ProgressBar.c < prev    next >
Text File  |  1994-04-28  |  8KB  |  242 lines

  1. /*****************************************************************************************************
  2. *                                                                                                    *
  3. * Finder ProgressBar.c - Copyright 1993 - 1994 Chris Larson, All rights reserved                     *
  4. *                                              (cklarson@engr.ucdavis.edu)                           *
  5. *                                                                                                    *
  6. * Source file of a CDEF which mimics the progress bar used in the Finder. This source file and its   *
  7. * compiled derivatives may be freely used within any freeware/shareware/postcardware/beerware/… as   *
  8. * long as you mention my name in your credits. Neither this source nor its compiled derivatives are  *
  9. * in the public domain and may not be use in any form in public domain software. Neither this source *
  10. * nor its compiled derivatives may be used in any form in a commercial product without the expressed,*
  11. * written consent of the author (me).                                                                *
  12. *                                                                                                    *
  13. * Version 1.1 -- April 28, 1994.                                                                     *
  14. *                                                                                                    *
  15. *****************************************************************************************************/
  16.  
  17. #include "Finder ProgressBar.h"
  18.  
  19. //----------------------------------------------------------------------------------------------------
  20. //
  21. // main -- Handle everything but draw messages.
  22. //
  23. //----------------------------------------------------------------------------------------------------
  24.  
  25. pascal long main (short varCode, ControlHandle theControlHandle, short message, long param)
  26. {
  27.     SignedByte    controlRecState;    // Holds the state of the control record’s handle.
  28.     long        returnValue;        // Holds the value returned by the CDEF.
  29.     ControlPtr    theControl;            // Holds a pointer to the control record.
  30.     
  31.     // ----------
  32.     // Since I use some static local variables I need to set up A4 so they can be accessed
  33.     // properly.
  34.     // ----------
  35.     
  36.     MySetUpA4();
  37.     
  38.     // ----------
  39.     // Lock down the control record.
  40.     // ----------
  41.     
  42.     controlRecState = HGetState((Handle)theControlHandle);
  43.     HLock((Handle)theControlHandle);
  44.     
  45.     // ----------
  46.     // Initialize the return value and the control pointer (since the record is locked it is safe
  47.     // to use a pointer). The return value is initialized to 0 since most CDEF messages want 0
  48.     // returned.
  49.     // ----------
  50.     
  51.     returnValue = 0;
  52.     theControl = *theControlHandle;
  53.     
  54.     switch (message)
  55.         {
  56.         
  57.         // ----------
  58.         // Ignore draw messages if the control is not visible.
  59.         // ----------
  60.         
  61.         case drawCntl:
  62.             if (theControl->contrlVis)
  63.                 DrawProgressBar(theControl);
  64.             break;
  65.         
  66.         // ----------
  67.         // Test the control: return 1 if the given point is within the control's rectangle
  68.         // and the control is not dimmed, 0 otherwise (the whole bar is part #1).
  69.         // ----------
  70.         
  71.         case testCntl:
  72.             if (theControl->contrlHilite != 0xFF)
  73.                 returnValue = PtInRect(*(Point*)(¶m),&(theControl->contrlRect));
  74.             break;
  75.         
  76.         // ----------
  77.         // For the 24-bit mode region calculation message, the high bit of the given region handle
  78.         // must be cleared. Note that I don’t test this bit to determine which region to calculate.
  79.         // Since this control has no indicator (part with a part code >= 129) I will never receive
  80.         // a message asking for indicator region calculation.
  81.         // ----------
  82.         
  83.         case calcCRgns:
  84.             param &= 0x7FFFFFFF;
  85.         
  86.         // ----------
  87.         // Calculate the control’s region.
  88.         // ----------
  89.         
  90.         case calcCntlRgn:
  91.             RectRgn((RgnHandle)param,&(theControl->contrlRect));
  92.             break;
  93.         
  94.         // ----------
  95.         // All other messages do nothing and return 0.
  96.         // ----------
  97.         
  98.         default:
  99.             break;
  100.         }
  101.     
  102.     // ----------
  103.     // Restore the state of the control record’s handle.
  104.     // ----------
  105.     
  106.     HSetState ((Handle)theControlHandle, controlRecState);
  107.     
  108.     // ----------
  109.     // Restore A4
  110.     // ----------
  111.     
  112.     MyRestoreA4();
  113.     
  114.     // ----------
  115.     // We’re outa here.
  116.     // ----------
  117.     
  118.     return (returnValue);
  119. }
  120.  
  121. //----------------------------------------------------------------------------------------------------
  122. //
  123. // DrawProgressBar -- Draw the control according to the values within the control record.
  124. //
  125. //----------------------------------------------------------------------------------------------------
  126.  
  127. void DrawProgressBar (ControlPtr theControl)
  128. {
  129.     PenState        oldPen;
  130.     RGBColor        oldFore, oldBack;
  131.     static RGBColor    blackColor = cBlack, whiteColor = cWhite, blueColor = cBlue, grayColor = cGray;
  132.     RgnHandle        oldClip,currentClip;
  133.     Rect            theBox = theControl->contrlRect;
  134.     GrafPtr            savePort, controlPort = theControl->contrlOwner;
  135.         
  136.     // ----------
  137.     // Set the port to the control’s port. Very probably not needed (since Apple’s CDEFs do not
  138.     // do this) but just to be extra safe…
  139.     // ----------
  140.     
  141.     GetPort (&savePort);
  142.     SetPort (controlPort);
  143.     
  144.     // ----------
  145.     // Save the current port’s clip region and set it to the intersection of the clip region with
  146.     // the control’s rectangle.
  147.     // ----------
  148.     
  149.     oldClip = NewRgn();
  150.     GetClip(oldClip);
  151.     
  152.     ClipRect(&theBox);
  153.     currentClip = theControl->contrlOwner->clipRgn;
  154.     SectRgn(currentClip,oldClip,currentClip);
  155.     
  156.     // ----------
  157.     // Save and reset the pen state
  158.     // ----------
  159.     
  160.     GetPenState(&oldPen);
  161.     PenNormal();
  162.     
  163.     // ----------
  164.     // If we have color QD, save the foreground and background colors and set the foreground color
  165.     // to black, background color to white.
  166.     // ----------
  167.     
  168.     if ( (ROM85 & kNoColorQD) == 0 )
  169.         {
  170.         GetForeColor(&oldFore);
  171.         GetBackColor(&oldBack);
  172.         
  173.         RGBForeColor(&blackColor);
  174.         RGBBackColor(&whiteColor);
  175.         }
  176.     
  177.     // ----------
  178.     // Draw the progress bar frame and set up the rectangle for drawing the bar.
  179.     // ----------
  180.     
  181.     FrameRect(&theBox);
  182.     InsetRect(&theBox,1,1);
  183.     
  184.     // ----------
  185.     // Calculate the location of the right end of the progress bar. (See header file.)
  186.     // ----------
  187.     
  188.     theBox.right = CalculateBarBoundry(theBox.left,theBox.right,theControl);
  189.                 
  190.     // ----------
  191.     // If we have color, set the foreground color to gray, leave the background white so that
  192.     // the gray will map to black on a 1-bit monitor. Note that for non-color QD machines, we want
  193.     // the bar to be black. Since the PenNormal() call set the pen pattern to black, we need do
  194.     // nothing for this case. Then draw the bar.
  195.     // ----------
  196.     
  197.     if ( (ROM85 & kNoColorQD) == 0 )
  198.         RGBForeColor(&grayColor);
  199.  
  200.     PaintRect(&theBox);
  201.     
  202.      // ----------
  203.      // Set up the rectangle to describe the space from the end of the bar to the edge of the
  204.      // inset rectangle.
  205.      // ----------
  206.      
  207.     theBox.left = theBox.right;
  208.     theBox.right = (theControl->contrlRect.right) - 1;
  209.  
  210.     // ----------
  211.     // If we have color, set the foreground to blue and the background to black (so that the blue
  212.     // will map to white on a 1-bit monitor). If no color, we want to erase the empty space, so
  213.     // set the pen to erase. Then paint the rectangle.
  214.     // ----------
  215.  
  216.     if ( (ROM85 & kNoColorQD) == 0 )
  217.         {
  218.         RGBForeColor(&blueColor);
  219.         RGBBackColor(&blackColor);
  220.         }
  221.     else
  222.         PenMode(patBic);
  223.     
  224.     PaintRect(&theBox);
  225.     
  226.     // ----------
  227.     // Restore all the stuff we munged: colors (if needed), pen state, clip region, and current port.
  228.     // ----------
  229.     
  230.     if ( (ROM85 & kNoColorQD) == 0 )
  231.         {
  232.         RGBForeColor(&oldFore);
  233.         RGBBackColor(&oldBack);
  234.         }
  235.     
  236.     SetPenState(&oldPen);
  237.     
  238.     SetClip(oldClip);
  239.     DisposeRgn(oldClip);
  240.     
  241.     SetPort(savePort);
  242. }